
package lnp;

import java.awt.Color;
import robocode.HitByBulletEvent;
import robocode.HitRobotEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;
import static robocode.util.Utils.normalRelativeAngleDegrees;

/**
 * The FiringDock class creates a robot that try to defeat eight simple robots, 
 * Corners, Crazy, Fire, RamFire, SittingDuck, SpinBot, Tracker, and Walls
 * which included in the Robocode package.
 *  
 * @author Lyneth Peou
 * @version 1.1 
 */

public class FiringDock extends Robot {  
  
  /**
   * Create a robot that rotate its radar constantly. 
   *  
   */
  public void run() {
    //Set colors
    this.setBodyColor(Color.orange);
    this.setGunColor(Color.black);
    this.setRadarColor(Color.green);
    this.setScanColor(Color.white);
    this.setBulletColor(Color.red);

    //Keep the gun still when we turn
    this.setAdjustGunForRobotTurn(true); 
    
    //Keep the radar still when turn the gun
    this.setAdjustRadarForGunTurn(true);
    
    while (true) {
      this.turnRadarRight(36);
      this.scan();
    }
  }

  /**
   * Makes the robot turn towards the enemy and fire with power proportionally 
   * to the enemy's distant and velocity. 
   * The robot also moves randomly by turn right 
   * then move back after each scan to avoid the bullet. 
   * 
   * @param e contains information about the enemy, e.g. location
   */
  public void onScannedRobot(ScannedRobotEvent e) {   
    
    //Calculate the enemy position.
    double absoluteBearing = this.getHeading() + e.getBearing();    
    
    double bearingFromRadar = normalRelativeAngleDegrees(absoluteBearing - 
                                this.getRadarHeading());    
    
    double bearingFromGun = normalRelativeAngleDegrees(absoluteBearing - this.getGunHeading());    
    
    this.turnRadarRight(bearingFromRadar); 
    
    this.turnGunRight(bearingFromGun );    
    
    if (e.getDistance() > 300) {
      if (e.getVelocity() > 0) {        
        this.fire(0.1);        
      }
      else {        
        this.fire(3);                
      }
    }
    else {      
      this.fire(3);      
    }
    
    this.randomMove(); 
         
    //Rescan for the enemy when it out of sight.
    if (bearingFromGun == 0) {
      this.scan();
    }      
  }
  
  /**
   * Make the robot turn the gun relatively to the enemy then fire hard. 
   * 
   * @param e contains information about the enemy robot, e.g. its location
   */
  
  public void onHitRobot(HitRobotEvent e) {    
    double turnGunAmt = normalRelativeAngleDegrees(e.getBearing() 
                          + this.getHeading() - this.getGunHeading());
    this.turnGunRight(turnGunAmt);
    this.fire(3);
  }  
  
  /**
   * Make the robot turn perpendicularly to the bullet then move a bit.
   * 
   * @param e contains information about the bullet, e.g. its power
   */
  public void onHitByBullet(HitByBulletEvent e) {
    turnRight(normalRelativeAngleDegrees(90 - (getHeading() - e.getHeading())));
    ahead(100);
    scan();
  }

  /**
   * Create a random movement by making the robot turn right 80 degree 
   * then moves all the ways back.  
   * 
   */
  public void randomMove() {    
    this.turnRight(80);
    this.back(510);     
  }
  
}
